﻿namespace DES
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class DESEncryption
    {
        private static byte[] Keys = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };

        public static string CutLeft(string originalVal, int cutLength)
        {
            if (string.IsNullOrEmpty(originalVal))
            {
                return string.Empty;
            }
            if (cutLength < 1)
            {
                return originalVal;
            }
            byte[] bytes = Encoding.Default.GetBytes(originalVal);
            if (bytes.Length <= cutLength)
            {
                return originalVal;
            }
            int length = cutLength;
            int[] numArray = new int[cutLength];
            byte[] destinationArray = null;
            int num2 = 0;
            for (int i = 0; i < cutLength; i++)
            {
                if (bytes[i] > 0x7f)
                {
                    num2++;
                    if (num2 == 3)
                    {
                        num2 = 1;
                    }
                }
                else
                {
                    num2 = 0;
                }
                numArray[i] = num2;
            }
            if ((bytes[cutLength - 1] > 0x7f) && (numArray[cutLength - 1] == 1))
            {
                length = cutLength + 1;
            }
            destinationArray = new byte[length];
            Array.Copy(bytes, destinationArray, length);
            return Encoding.Default.GetString(destinationArray);
        }
        public static string Decrypt(string decryptString, string decryptKey)
        {
            decryptKey = CutLeft(decryptKey, 8);
            decryptKey = decryptKey.PadRight(8, ' ');
            byte[] bytes = Encoding.UTF8.GetBytes(decryptKey);
            byte[] keys = Keys;
            byte[] buffer = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, keys), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Encoding.UTF8.GetString(stream.ToArray());
        }

        public static string Encrypt(string encryptString, string encryptKey)
        {
            encryptKey = CutLeft(encryptKey, 8);
            encryptKey = encryptKey.PadRight(8, ' ');
            byte[] bytes = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] keys = Keys;
            byte[] buffer = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(bytes, keys), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Convert.ToBase64String(stream.ToArray());
        }
    }
}

